package com.cqu.hospitalsystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.cqu.hospitalsystem.entity.Doctor;
import com.cqu.hospitalsystem.entity.Office;
import com.cqu.hospitalsystem.service.DoctorService;
import com.cqu.hospitalsystem.utils.result.DataResult;
import com.cqu.hospitalsystem.utils.result.code.Code;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("doctor")
@Tag(name = "DoctorController", description = "医生管理")
public class DoctorController {
    /**
     * 服务对象
     */
    @Resource
    private DoctorService doctorService;

    @PostMapping("loginDoctor")
    @Operation(summary = "医生登录", description = "根据医生手机号和密码进行登录")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "登录成功"),
            @ApiResponse(responseCode = "400", description = "登录失败")
    })
    public DataResult loginDoctor(@RequestBody @Parameter(description = "医生登录信息") Doctor doctor) {
        System.out.println("loginDoctor：");
        System.out.println("这是前端传回的信息：");
        System.out.println(doctor.getDocPhone());
        System.out.println(doctor.getDocPassword());
        Doctor loginDoctor = this.doctorService.loginDoctor(doctor);
        System.out.println("这是后端返回的信息：");
        System.out.println(loginDoctor);
        if (loginDoctor != null) {
            DataResult dataResult = DataResult.successByData(loginDoctor);
            dataResult.setcode(666);
            return dataResult;
        } else {
            return DataResult.errByErrCode(Code.ACCOUNT_ERROR);
        }
    }

    @PostMapping("docInfo")
    @Operation(summary = "获取医生信息", description = "根据医生ID获取医生信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "医生不存在")
    })
    public DataResult docInfo(@RequestBody @Parameter(description = "医生对象") Doctor doctor) {
        System.out.println("docInfo");
        System.out.println(doctor.getDocId());
        Doctor doctor1 = this.doctorService.queryById(doctor.getDocId());
        if (doctor1 != null) {
            DataResult dataResult = DataResult.successByData(doctor1);
            dataResult.setcode(666);
            return dataResult;
        } else {
            return DataResult.errByErrCode(Code.ACCOUNT_ERROR);
        }
    }

    @PostMapping("editDocPwd")
    @Operation(summary = "编辑医生密码", description = "根据医生ID编辑医生密码")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "编辑成功"),
            @ApiResponse(responseCode = "400", description = "编辑失败")
    })
    public DataResult editDocPwd(@RequestBody @Parameter(description = "医生对象") Doctor doctor) {
        System.out.println("editDocPwd");
        System.out.println(doctor.getDocId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("isSuccess", this.doctorService.editDocPwd(doctor));
        System.out.println(jsonObject);
        return DataResult.successByData(jsonObject);
    }

    @PostMapping("editDocInfo")
    @Operation(summary = "编辑医生信息", description = "根据医生ID编辑医生信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "编辑成功"),
            @ApiResponse(responseCode = "400", description = "编辑失败")
    })
    public DataResult editDocInfo(@RequestBody @Parameter(description = "医生对象") Doctor doctor) {
        System.out.println("editDocInfo");
        System.out.println(doctor.getDocId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("isSuccess", this.doctorService.editDocInfo(doctor));
        System.out.println(jsonObject);
        return DataResult.successByData(jsonObject);
    }

    @PostMapping("searchAvailable")
    @Operation(summary = "搜索可用医生", description = "根据科室ID和预约时间搜索可用医生")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "无可用医生")
    })
    public DataResult searchAvailable(@RequestBody @Parameter(description = "搜索参数") Map<String, Object> param) throws ParseException {
        System.out.println("searchAvailable");
        long officeId = Integer.parseInt(param.get("officeId").toString());
        String appoint = param.get("appointTime").toString();
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date appointTime = fmt.parse(appoint);

        List<Doctor> doctorList = this.doctorService.searchAvailable(officeId, appointTime);
        JSONObject jsonObject = new JSONObject();
        if (doctorList != null) {
            jsonObject.put("doctorList", doctorList);
            DataResult dataResult = DataResult.successByData(jsonObject);
            dataResult.setcode(666);
            return dataResult;
        } else {
            return DataResult.errByErrCode(Code.ACCOUNT_ERROR);
        }
    }

    @GetMapping
    @Operation(summary = "分页查询医生", description = "根据筛选条件和分页信息查询医生")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "400", description = "查询失败")
    })
    public ResponseEntity<Page<Doctor>> queryByPage(Doctor doctor, PageRequest pageRequest) {
        return ResponseEntity.ok(this.doctorService.queryByPage(doctor, pageRequest));
    }

    @GetMapping("{id}")
    @Operation(summary = "根据ID查询医生", description = "根据医生ID查询单个医生")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "医生不存在")
    })
    public ResponseEntity<Doctor> queryById(@PathVariable("id") @Parameter(description = "医生ID") Long id) {
        return ResponseEntity.ok(this.doctorService.queryById(id));
    }

    @PostMapping
    @Operation(summary = "新增医生", description = "创建新的医生")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "创建失败")
    })
    public ResponseEntity<Doctor> add(@RequestBody @Parameter(description = "医生对象") Doctor doctor) {
        return ResponseEntity.ok(this.doctorService.insert(doctor));
    }

    @PutMapping
    @Operation(summary = "编辑医生", description = "更新医生信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "更新失败")
    })
    public ResponseEntity<Doctor> edit(@RequestBody @Parameter(description = "医生对象") Doctor doctor) {
        return ResponseEntity.ok(this.doctorService.update(doctor));
    }

    @DeleteMapping
    @Operation(summary = "删除医生", description = "根据医生ID删除医生")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "删除失败")
    })
    public ResponseEntity<Boolean> deleteById(@Parameter(description = "医生ID") Long id) {
        return ResponseEntity.ok(this.doctorService.deleteById(id));
    }

    @PostMapping("queryById")
    @Operation(summary = "根据ID查询医生信息", description = "根据医生ID查询医生信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "医生不存在")
    })
    public DataResult queryById(@RequestBody @Parameter(description = "医生对象") Doctor doctor) {
        System.out.println(doctor.getDocId());
        Doctor doctor1 = this.doctorService.queryById(doctor.getDocId());
        DataResult dataResult = DataResult.successByData(doctor1);
        dataResult.setcode(666);
        return dataResult;
    }

    @PostMapping("searchDocList")
    @Operation(summary = "搜索医生列表", description = "根据科室名称搜索医生列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "无医生列表")
    })
    public DataResult searchDocList(@RequestBody @Parameter(description = "科室对象") Office office) {
        return DataResult.successByDataArray(doctorService.searchDocList(office.getOfficeName()));
    }
    @PostMapping("docDetail")
    @Operation(summary = "获取医生详情", description = "根据医生ID获取医生详情")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "医生不存在")
    })

    public DataResult docDetail(@RequestBody @Parameter(description = "医生对象") Doctor doctor){
        return DataResult.successByData(doctorService.docDetail(doctor.getDocId()));
    }
}